import { Component, OnInit } from '@angular/core';
import {NoticeService} from '../notice.service';
import { Router} from '@angular/router';
import { MdlSnackbarService } from '@angular-mdl/core';
import { ConfirmationService } from 'primeng/primeng';

@Component({
  selector: 'app-publish-list',
  templateUrl: './publish-list.component.html',
  styleUrls: ['./publish-list.component.scss'],
    providers: [NoticeService]
})

export class PublishListComponent implements OnInit {
    humanId: any;
    title: any;
    publishList: any[];
    totalRecords: number = 0;
    text: string;
    selectNotice: any; // 当前选中的实列
    url: string;
    urlList: any[] = [];
    isShow: boolean = false;

  constructor(
      private router: Router,
      private noticeService: NoticeService,
      private mdlSnackbarService: MdlSnackbarService,
      private confirmationService: ConfirmationService) { }

  ngOnInit() {
      let sysConfigMap = JSON.parse(localStorage.getItem("sysConfigMap"));
      let currentHumanInfo = JSON.parse(localStorage.getItem('currentHumanInfo'));
      this.humanId = currentHumanInfo.humanId;
      this.searchByTitle(null);
  }

    /** 加载通知数据 */
    loadData(params) {
        this.noticeService.loadPublishList(params).subscribe(data => {
            this.publishList = data ? data.list : [];
            this.totalRecords = data ? data.total : 0;
            if (this.publishList.length > 0) {
                this.selectNotice = this.publishList[0];
                this.readNotice(this.selectNotice);
                this.isShow = false;
            } else {
                this.isShow = true;
            }
        });
    }

    /** 通过标题模糊查找 */
    searchByTitle(event: string) {
        this.text = null;
        let params = {};
        params['humanId'] = this.humanId;
        params['title'] = event ? event : '';
        params['pageSize'] = 15;
        params['pageNum'] = 1;
        this.loadData(params);
    }

    /** 点击加载下一页数据 */
    loadNoticeList(event) {
        let params = {};
        params['humanId'] = this.humanId;
        params['title'] = this.title ? this.title : '';
        params['pageSize'] = 15;
        params['pageNum'] = event ? event.first / event.rows + 1 : 1;
        this.loadData(params);
    }

    /** 改变选中实例的颜色 */
    readNotice(event) {
        this.publishList.forEach(e => {
            if (e.noticeId === event.noticeId) {
                this.selectNotice = e;
                e['style'] = {'background-color': '#007ad9', 'color': '#FFFFFF', 'cursor': 'pointer'};
            } else {
                e['style'] = {'background-color': '#FFFFFF', 'color': '#333333', 'cursor': 'pointer'};
            }
        });
        this.urlList = [];
        let idList = [];
        if (this.selectNotice.attachmentIds != '' && this.selectNotice.attachmentIds != null) {
            idList = this.selectNotice.attachmentIds.split('/');
            this.noticeService.getUrlById(idList).subscribe( data => this.urlList = data);
        }
            this.text = '<h1 class="ql-align-center">' + event.title + '</h1>' + '<p style="text-align: right;">' + event.createTime + '&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;' + event.publishUnit + '</p>' + event.content;
    }

    /** 新增通知消息 */
    addNotice() {
        this.router.navigate(['/notification/edit']);
    }

    /** 删除选中实例 */
    deleteNotice() {
        this.confirmationService.confirm({
            header: '确认',
            message: '您确定要删除该条公告吗？',
            accept: () => {
                this.noticeService.deleteNoticeById(this.selectNotice.noticeId).subscribe(data => {
                    if (data) {
                        this.mdlSnackbarService.showToast('删除成功！');
                        this.searchByTitle(null);
                    }
                });
            }
        });
    }

    /** 回车键搜索 */
    enterOnkeydown($event) {
        if ($event.keyCode == 13) {
            this.searchByTitle(this.title);
        }
    }

    /** 附件下载 */
    download(event) {
        this.noticeService.downland(event.mediaPath, event.mediaName).subscribe();
    }

    /** 编辑 **/
    editNotice() {
        this.router.navigate(['/notification/edit', {'noticeId': this.selectNotice.noticeId}]);
    }
}
